package org.example.treasure;

import org.example.Window.MainWindow;
import org.example.Window.MessageDialog;
import org.example.path.Path;
import org.example.player.Player;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

public class Treasure extends JButton{
    public String ID;           //部落ID
    public String name;         //部落名称
    public String description;  //部落简介
    public int value;                  //部落价值
    public int dist = Integer.MAX_VALUE;    //部落最短距离
    public boolean isVisible = false; //在算法中是否被访问过
    public int visitedNum = 0; //在算法中是否被访问过
    public ArrayList<Treasure> route = new ArrayList<>();;   //答案部落路径
    ImageIcon image = new ImageIcon("src/main/java/org/example/treasure/部落.png");
    ImageIcon icon;
    public ArrayList<Path> paths = new ArrayList<>();



    public Treasure(int x, int y, String ID, int value, String name) {
        this.name = name;
        icon = new ImageIcon("src/treasure image/"+this.name+".png");
        this.ID = ID;
        this.value = value;
        this.setIcon(image);
        this.setBounds(x,y,82,70);
        this.setOpaque(false);
        this.setBorderPainted(false);
        this.setContentAreaFilled(false);
        this.addMouseListener(new treasureListener());
    }
    public Treasure(int x, int y, String ID, int value, String name, Boolean answer) {
        this.name = name;
        icon = new ImageIcon("src/treasure image/"+this.name+".png");
        this.ID = ID;
        this.value = value;
        this.setIcon(image);
        this.setBounds(x,y,82,70);
        this.setOpaque(false);
        this.setBorderPainted(false);
        this.setContentAreaFilled(false);
        this.addMouseListener(new answerListener());
    }

    public void addPath(Path path) {
        paths.add(0,path);
    }
    public ArrayList<Path> getPaths() {
        return paths;
    }
    public void setDescription(String description) {
        this.description = description;
    }

    public ImageIcon getIconImage() {
        return icon;
    }
    static class treasureListener implements MouseListener{

        public void move(int x,int y){
            Player player = new Player();

            Player.jPanel.removeAll();
            Player.jPanel.revalidate();
            Player.jPanel.repaint();

            player.setBounds(x - 40,y - 10,75,50);
            Player.jPanel.add(player);
        }
        @Override
        public void mouseClicked(MouseEvent e) {
            Treasure treasure = (Treasure) e.getSource();

            for (Path path : Player.current.paths) {
                System.out.println(path.to+" jiancha  "+treasure.ID);
                if (path.to.equals(treasure.ID)){
                    move(treasure.getX(), treasure.getY());
                    //更新生命和经济数值
                    JPanel playerData = MainWindow.playerData;
                    playerData.removeAll();
                    playerData.revalidate();
                    playerData.repaint();
                    if (path.damage!=2){
                        MainWindow.life -= path.damage;
                    }
                    MainWindow.money += treasure.value;
                    treasure.value = 0;
                    Player.current = treasure;
                    Player.route.add(Player.current);
                    MainWindow.creatLifeAndMoney(playerData);
                    /*
                        处理结算页面
                     */
                    if (MainWindow.life <= 0){
                        MainWindow.messageDialog = new MessageDialog(MainWindow.money);
                        MainWindow.messageDialog.setVisible(true);
                    } else if (Player.current.ID.equals("O")) {
                        MainWindow.messageDialog = new MessageDialog(MainWindow.life,MainWindow.money);
                        MainWindow.messageDialog.setVisible(true);
                    }


                    return;
                }
            }

        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            Treasure treasure = (Treasure) e.getSource();

            //更新生命和经济数值
            JPanel imfData = MainWindow.imfPanel;
            imfData.removeAll();
            imfData.revalidate();
            imfData.repaint();
            MainWindow.creatImf(imfData,treasure);

            treasure.setBorderPainted(true);
        }

        @Override
        public void mouseExited(MouseEvent e) {
            Treasure treasure = (Treasure) e.getSource();

            treasure.setBorderPainted(false);
        }
    }

    static class answerListener implements MouseListener{

        @Override
        public void mouseClicked(MouseEvent e) {
//            Treasure treasure = (Treasure) e.getSource();
//
//            for (Path path : Player.current.paths) {
//                System.out.println(path.to+" jiancha  "+treasure.ID);
//                if (path.to.equals(treasure.ID)){
//                    move(treasure.getX(), treasure.getY());
//                    //更新生命和经济数值
//                    JPanel playerData = MainWindow.playerData;
//                    playerData.removeAll();
//                    playerData.revalidate();
//                    playerData.repaint();
//                    MainWindow.life -= path.damage;
//                    MainWindow.money += treasure.value;
//                    treasure.value = 0;
//                    Player.current = treasure;
//                    Player.route.add(Player.current);
//                    MainWindow.creatLifeAndMoney(playerData);
//                    /*
//                        处理结算页面
//                     */
//                    if (MainWindow.life <= 0){
//                        MainWindow.messageDialog = new MessageDialog(MainWindow.money);
//                        MainWindow.messageDialog.setVisible(true);
//                    } else if (Player.current.ID.equals("O")) {
//                        MainWindow.messageDialog = new MessageDialog(MainWindow.life,MainWindow.money);
//                        MainWindow.messageDialog.setVisible(true);
//                    }
//
//
//                    return;
//                }
//            }

        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            Treasure treasure = (Treasure) e.getSource();
            treasure.setBorderPainted(true);
        }

        @Override
        public void mouseExited(MouseEvent e) {
            Treasure treasure = (Treasure) e.getSource();

            treasure.setBorderPainted(false);
        }
    }

}
